package com.android.mms.ui;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;

import com.android.mms.model.ImageModel;
import com.google.android.mms.pdu.PduPart;
import com.google.android.mms.util.SqliteWrapper;

public class UriImage {
	private static final boolean DEBUG = true;
	private static final boolean LOCAL_LOGV = true;
	private static final String TAG = "UriImage";
	private String mContentType;
	private final Context mContext;
	private int mHeight;
	private String mPath;
	private String mSrc;
	private final Uri mUri;
	private int mWidth;

	public UriImage(Context var1, Uri var2) {
		if (var1 != null && var2 != null) {
			if (var2.getScheme().equals("content")) {
				this.initFromContentUri(var1, var2);
			} else if (var2.getScheme().equals("file")) {
				this.initFromFile(var1, var2);
			}

			this.mSrc = this.mPath.substring(1 + this.mPath.lastIndexOf(47));
			this.mSrc = this.mSrc.replace(' ', '_');
			this.mContext = var1;
			this.mUri = var2;
			this.decodeBoundsInfo();
		} else {
			throw new IllegalArgumentException();
		}
	}

	private void decodeBoundsInfo() {
		InputStream var1 = null;

		try {
			var1 = this.mContext.getContentResolver().openInputStream(this.mUri);
			Options var9 = new Options();
			var9.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(var1, (Rect) null, var9);
			this.mWidth = var9.outWidth;
			this.mHeight = var9.outHeight;
			return;
		} catch (FileNotFoundException var18) {
			Log.e("UriImage", "IOException caught while opening stream", var18);
		} finally {
			if (var1 != null) {
				try {
					var1.close();
				} catch (IOException var17) {
					Log.e("UriImage", "IOException caught while closing stream", var17);
				}
			}

		}

	}

	private byte[] getResizedImageData(int widthLimit, int heightLimit) {
		int outWidth = mWidth;
		int outHeight = mHeight;
		int s = 1;
		while ((outWidth / s > widthLimit) || (outHeight / s > heightLimit)) {
			s *= 2;
		}
		if (LOCAL_LOGV) {
			Log.v(TAG, "outWidth=" + outWidth / s + " outHeight=" + outHeight / s);
		}
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = s;
		InputStream input = null;
		try {
			input = mContext.getContentResolver().openInputStream(mUri);
			Bitmap b = BitmapFactory.decodeStream(input, null, options);
			if (b == null) {
				return null;
			}
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			b.compress(CompressFormat.JPEG, 100, os);
			return os.toByteArray();
		} catch (FileNotFoundException e) {
			Log.e(TAG, e.getMessage(), e);
			return null;
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					Log.e(TAG, e.getMessage(), e);
				}
			}
		}
	}

	private void initFromContentUri(Context var1, Uri var2) {
		Cursor var3 = SqliteWrapper.query(var1, var1.getContentResolver(), var2, (String[]) null, (String) null,
				(String[]) null, (String) null);
		if (var3 == null) {
			throw new IllegalArgumentException("Query on " + var2 + " returns null result.");
		} else {
			try {
				if (var3.getCount() != 1 || !var3.moveToFirst()) {
					throw new IllegalArgumentException("Query on " + var2 + " returns 0 or multiple rows.");
				}

				String var5;
				if (ImageModel.isMmsUri(var2)) {
					var5 = var3.getString(var3.getColumnIndexOrThrow("fn"));
					if (TextUtils.isEmpty(var5)) {
						var5 = var3.getString(var3.getColumnIndexOrThrow("_data"));
					}

					this.mContentType = var3.getString(var3.getColumnIndexOrThrow("ct"));
				} else {
					var5 = var3.getString(var3.getColumnIndexOrThrow("_data"));
					this.mContentType = var3.getString(var3.getColumnIndexOrThrow("mime_type"));
				}

				this.mPath = var5;
			} finally {
				var3.close();
			}

		}
	}

	private void initFromFile(Context var1, Uri var2) {
		this.mContentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
				MimeTypeMap.getFileExtensionFromUrl(var2.toString()));
		if (this.mContentType == null) {
			throw new IllegalArgumentException("Unable to determine extension for " + var2.toString());
		} else {
			this.mPath = var2.getPath();
		}
	}

	public String getContentType() {
		return this.mContentType;
	}

	public int getHeight() {
		return this.mHeight;
	}

	public PduPart getResizedImageAsPart(int var1, int var2) {
		PduPart var3 = new PduPart();
		byte[] var4 = this.getResizedImageData(var1, var2);
		if (var4 == null) {
			Log.v("UriImage", "Resize image failed.");
			return null;
		} else {
			var3.setData(var4);
			var3.setContentType(this.getContentType().getBytes());
			String var5 = this.getSrc();
			byte[] var6 = var5.getBytes();
			var3.setContentLocation(var6);
			var3.setFilename(var6);
			var3.setContentId(var5.substring(0, var5.lastIndexOf(".")).getBytes());
			return var3;
		}
	}

	public String getSrc() {
		return this.mSrc;
	}

	public int getWidth() {
		return this.mWidth;
	}

}
